package com.spiro.framed;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

// Generated code
import java.io.IOException;

import org.apache.thrift.TException;
import org.apache.thrift.async.AsyncMethodCallback;
import org.apache.thrift.async.TAsyncClientManager;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TNonblockingSocket;
import org.apache.thrift.transport.TNonblockingTransport;

import com.spiro.common.tutorial.Calculator;

public class JavaAsyncClient {
    public static void main(String[] args) {

        if (args.length != 2) {
            System.out.println("Useage: com.spiro.ssl.JavaClient <server_ip> <server_port>");
            System.exit(0);
        }

        try {
            TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
            TNonblockingTransport transport = new TNonblockingSocket(args[0],
                    Integer.valueOf(args[1]));
            TAsyncClientManager clientManager = new TAsyncClientManager();
            Calculator.AsyncClient asyncClient = new Calculator.AsyncClient(
                    protocolFactory, clientManager, transport);
            System.out.println("Client calls .....");

            perform(asyncClient);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TException e) {
            // TODO Auto-bintest catch block
            e.printStackTrace();
        }

    }

    private static void perform(Calculator.AsyncClient asyncClient)
            throws TException {
        
         asyncClient
                .ping(new AsyncMethodCallback<Calculator.AsyncClient.ping_call>() {

                    @Override
                    public void onComplete(
                            Calculator.AsyncClient.ping_call response) {
                        System.out.println("Async => ping()");
                    }

                    @Override
                    public void onError(Exception exception) {
                        exception.printStackTrace();
                    }
                });
         
         try {
             Thread.sleep(5000);
         } catch (InterruptedException e) {
             // TODO Auto-bintest catch block
             e.printStackTrace();
         }        
        AsyncMethodCallback addCallback = new AsyncMethodCallback<Calculator.AsyncClient.add_call>() {

            @Override
            public void onComplete(Calculator.AsyncClient.add_call response) {
                try {
                    System.out.println("Async => add:" + response.getResult());
                } catch (TException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Exception e) {
                e.printStackTrace();
            }

        };

        asyncClient.add(1, 1, addCallback);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-bintest catch block
            e.printStackTrace();
        }    
        
        /*
        AsyncMethodCallback calcCallback = new AsyncMethodCallback<Calculator.AsyncClient.calculate_call>() {

            @Override
            public void onComplete(
                    Calculator.AsyncClient.calculate_call response) {
                try {
                    System.out.println("Async => calc:" + response.getResult());
                } catch (InvalidOperation e) {
                    // TODO Auto-bintest catch block
                    e.printStackTrace();
                } catch (TException e) {
                    // TODO Auto-bintest catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Exception e) {
                e.printStackTrace();
            }

        };

        Work work = new Work();

        work.op = Operation.DIVIDE;
        work.num1 = 1;
        work.num2 = 0;

        System.out.println("devide");
        asyncClient.calculate(1, work, calcCallback);

        work.op = Operation.SUBTRACT;
        work.num1 = 15;
        work.num2 = 10;
        asyncClient.calculate(1, work, calcCallback);

        AsyncMethodCallback structCallback = new AsyncMethodCallback<SharedService.AsyncClient.getStruct_call>() {

            @Override
            public void onComplete(
                    SharedService.AsyncClient.getStruct_call response) {
                try {
                    System.out
                            .println("Async => getStruct:" + response.getResult());
                } catch (TException e) {
                    // TODO Auto-bintest catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Exception e) {
                e.printStackTrace();
            }

        };
        asyncClient.getStruct(1, structCallback);
*/
    }
}
